<template>
  <el-input
    v-model="inputValue"
    v-bind="$attrs"
    :type="inputType"
    :maxlength="maxlength"
    :show-word-limit="showWordLimit"
    :clearable="clearable"
    :prefix-icon="prefixIcon"
    :suffix-icon="suffixIcon"
    :readonly="readonly"
    @change="handleChange"
    @focus="handleFocus"
    @blur="handleBlur"
    @input="handleInput"
  />
</template>

<script setup>
import { computed } from 'vue'
import { ElInput } from 'element-plus'

const props = defineProps({
  modelValue: [String, Number],
  type: {
    type: String,
    default: 'text'
  },
  maxlength: Number,
  showWordLimit: Boolean,
  clearable: {
    type: Boolean,
    default: true
  },
  prefixIcon: String,
  suffixIcon: String,
  readonly: Boolean
})

const emit = defineEmits(['update:modelValue', 'change', 'focus', 'blur', 'input'])

const inputValue = computed({
  get() {
    return props.modelValue
  },
  set(value) {
    emit('update:modelValue', value)
  }
})

const inputType = computed(() => {
  // 支持常见的input类型
  const supportedTypes = ['text', 'password', 'email', 'url', 'tel', 'search']
  return supportedTypes.includes(props.type) ? props.type : 'text'
})

const handleChange = (value) => {
  emit('change', value)
}

const handleFocus = (event) => {
  emit('focus', event)
}

const handleBlur = (event) => {
  emit('blur', event)
}

const handleInput = (value) => {
  emit('input', value)
}
</script>